<script lang="ts" setup>
import { reactive, ref } from "vue";
import { usePaging } from "@/hooks/usePaging";
import { ElMessage } from "element-plus";
import { Apis } from "@/apis";
import FuncShowConfirmBox from "@/utils/confirmBox";
import searchFrom from "@/components/funcs/searchForm/searchFrom.vue";
const ApiParams: { username: string } = reactive({
  username: "",
});
const {
  data,
  pages,
  total,
  pagesize,
  changePage,
  filterHandler,
  refreshData,
  getData,
} = usePaging(Apis.getAllUsers, ApiParams, 10);
//---
const rolesArr: {
  [key: string]: { color: string; name: string; value: string };
} = {
  jyu_admin: { color: "#67c23a", name: "管理员", value: "1" },
  user: { color: "#000000", name: "普通用户", value: "0" },
  warner: { color: "#f56c6c", name: "封禁用户", value: "2" },
};
//---
const optionsValue = ref<string>("");
const options: { value: string; label: string; role: string }[] = [
  {
    value: "0",
    label: "普通用户",
    role: "user",
  },
  {
    value: "1",
    label: "管理员",
    role: "jyu_admin",
  },
  {
    value: "2",
    label: "封禁用户",
    role: "warner",
  },
];
const currentEditIndex = ref<number>(-1);
const changeRole = (name: string, id: number): void => {
  console.log(name, optionsValue.value, id);
  FuncShowConfirmBox(
    `确认将用户“${name}”的账号状态更改为"${
      options[+optionsValue.value].label
    }"吗？`,
    () => {
      Apis.changeRole({
        id: id.toString(),
        role: options[+optionsValue.value].role,
      }).then((res) => {
        console.log(res);
        if (res.status === 200) {
          getData();
          ElMessage.success("修改成功");
          currentEditIndex.value = -1;
        }
      });
    },()=>{
      currentEditIndex.value = -1;
    }
  );
};
const editRole = (index: number, role: string): void => {
  console.log(index, role);
  optionsValue.value = rolesArr[role].value;
  currentEditIndex.value = index;
};
</script>
<template>
  <div class="conForm">
    <div class="conTitle">
      <span>用户列表</span>
      <searchFrom
        v-model="ApiParams.username"
        @funcSearch="refreshData"
        placeholder="输入用户名"
      ></searchFrom>
    </div>
    <el-table
      :data="data"
      stripe
      style="width: 100%"
      height="56.8vh"
      highlight-current-row
      border
    >
      <el-table-column label="头像" align="center">
        <template #default="scope">
          <div
            style="display: flex; justify-content: center; align-items: center"
          >
            <el-avatar :size="35" :src="scope.row.avatarUrl" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="账号id" prop="id" sortable align="center">
      </el-table-column>
      <el-table-column label="用户名" prop="username" align="center">
      </el-table-column>
      <el-table-column label="密码" prop="password" align="center">
      </el-table-column>
      <el-table-column label="发帖数量" prop="postNums" align="center" sortable>
      </el-table-column>
      <el-table-column
        label="角色"
        prop="role"
        align="center"
        :filters="[
          { text: '管理员', value: 'jyu_admin' },
          { text: '普通用户', value: 'user' },
          { text: '禁言用户', value: 'warn' },
        ]"
        :filter-method="filterHandler"
      >
        <template #default="scope">
          <div v-if="scope.$index === currentEditIndex" class="editInput">
            <span>
              <el-select
                v-model="optionsValue"
                placeholder="选择"
                size="large"
                style="width: 60%"
                :focus="true"
                @change="changeRole(scope.row.username, scope.row.id)"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </span>
            <span class="icon" @click="currentEditIndex = -1">
              <el-icon><CloseBold /></el-icon>
            </span>
          </div>
          <div
            :style="`color: ${
              rolesArr[scope.row.role].color
            };font-weight: 550;`"
            class="role"
            v-else
          >
            {{ rolesArr[scope.row.role].name }}
            <span @click="editRole(scope.$index, scope.row.role)">
              <el-icon><Edit /></el-icon>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div
      style="
        display: flex;
        flex-direction: row-reverse;
        margin-right: 20px;
        height: 6vh;
      "
    >
      <el-pagination
        v-model:current-page="pages"
        size="large"
        layout="total,prev, pager, next, jumper"
        :total="total"
        :page-size="pagesize"
        @current-change="changePage"
      />
    </div>
  </div>
</template>
<style lang="scss" scoped>
.conForm {
  height: 62vh;
  background-color: #fff;
  margin-top: 10px;
  .conTitle {
    height: 10%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span {
      margin-left: 20px;
      font-size: 22px;
      font-weight: 700;
    }
  }
  .role {
    position: relative;
    text-align: center;
    span {
      width: 30px;
      position: absolute;
      font-size: 16px;
      color: #949699;
      right: 20px;
      cursor: pointer;
      &:hover {
        color: #409eff;
      }
    }
  }
  .editInput {
    position: relative;
    text-align: center;
    .icon {
      width: 30px;
      position: absolute;
      font-size: 16px;
      color: #949699;
      right: 10px;
      top: 50%;
      transform: translateY(-50%);
      cursor: pointer;
      &:hover {
        color: #409eff;
      }
    }
  }
}
</style>
